//覆盖$.init初始化函数定义
+function ($) {
    "use strict";

    $(document).on('ajaxError', function(e) {
        if (e._args && e._args[2] === 'BAD REQUEST') {
            var current_location = window.location + ''
            if (window.location.port) {
                current_location = current_location.replace(':' + window.location.port, '');
            }

            $.ajax({
                url: '/mshop/get_enterprise_info',
                type: 'get',
                success: function(e) {
                    var args = JSON.parse(e);
                    if (args.appid !== undefined) {
                        var request_host = 'https://open.weixin.qq.com/connect/oauth2/authorize',
                        request_appid = '?appid=' + args.appid,
                        request_uri = '&redirect_uri=' + current_location,
                        request_staet = "&state=('db': '" + args.dbname + "', 'agentid': " + args.agentid + ",'no_binding': 1, 'login': 1)#wechat_redirect"

                        window.location = request_host + request_appid + request_uri + "&response_type=code&scope=snsapi_base" + request_staet;
                    } else {
                        alert('抱歉，请先配置后端的企业号设置')
                    }
                }
            })
        };
    });

    var getPage = function() {
        var $page = $(".page-current");
        if(!$page[0]) $page = $(".page").addClass("page-current");
        return $page;
    };

    var getData = function(){
        var json_list = $("script[type='text/json']");
        var data;
        for (var i = 0; i < json_list.length; i ++){
            var json_text = json_list[i];
            var json;
            try{
                json = JSON.parse(json_text.innerText);
            }
            catch(e){
                throw new Error("text/json ID: " + json_text.id + ", ERROR: " + e.message);
            }
            if (json){
                if (!data) data = {};
                json['$id'] = json_text.id;
                data[json_text.id] = json;
            }
        }
        return data;
    };

    $.init = function() {
        var $page = getPage();
        var id = $page[0].id;
        var id_list = $("div[id='" + id + "']");
        if (id_list.length > 1){
            id_list[0].remove();
        }
        $.initPage();
        var data = getData();

        //动态加载的内容需要scan后才能生效，当前页面直接刷新的话不需要手动scan(avalon会自动scan)
        if ($page.data("page-remote")){
            //动态加载的页面不会自动执行里面的javascript，eval执行
            var script_list = $('#' + id + " script[type='application/javascript']");
            var memory_script = [];
            for (var i = 0; i < script_list.length; i ++){
                var script = script_list[i];
                if (!memory_script[script_list[i]]) {
                    eval($(script).text());
                    memory_script[script_list[i]] = true;
                }

            }
        }

        $page.trigger("pageInit", [id, $page, data]);

    };

}(Zepto);


